import os
from xml.dom import minidom

from util.PathUtils import PathUtils

path = PathUtils.getCurrentPath()
parent = PathUtils.getParentPath(path)
parent = "/Users/cai/AndroidProjects/sxwPad/UserOrder/src/main/res/drawable-xhdpi"
targetPath = "/Users/cai/AndroidProjects/sxwPad/UserOrder/src/main/res/drawable"

statesTuple = ("normal", "disable", "pressed", "selected")


class MakeFile:
    file_name = ""

    def __init__(self, f: str):
        self.file_name = f

    def is_img(self):
        return self.file_name.endswith(".png")

    def get_prefix(self):
        index = self.file_name.rfind("_")
        return self.file_name[0:index]

    def get_type(self):
        index = self.file_name.rfind("_")
        t = self.file_name[index + 1:]
        t = t.replace("." + self.get_ext(), "")
        return t

    def get_ext(self):
        index = self.file_name.rfind(".")
        name = self.file_name[index + 1:]
        return name

    def no_ext_name(self):
        index = self.file_name.rfind(".")
        return self.file_name[0:index]

    def isstate(self):
        if statesTuple.__contains__(self.get_type()):
            return True
        return False


class State:
    value = ""
    file_name = ""

    def __lt__(self, other):
        return not self.value == "normal"


class Selector:
    filename = ""

    # key = state ; value = name

    def __init__(self, prefix):
        self.prefix = prefix
        self.filename = prefix + "_selector.xml"
        self.states = list()

    def add_state(self, value, name):
        s = State()
        s.value = value
        s.file_name = name
        self.states.append(s)
        self.sort()

    def sort(self):
        self.states.sort()


di = dict()


def handleTheFile(f: str):
    mf = MakeFile(f)
    if mf.is_img():
        prefix = mf.get_prefix()
        t = mf.get_type()
        ext = mf.get_ext()
        if not mf.isstate():
            return
        createSelector(mf, prefix, t)
    pass


def createSelector(mf, prefix, t):
    bean: Selector = di.get(prefix, None)
    if bean is None:
        bean = Selector(prefix)
        di[prefix] = bean
    bean.add_state(t, mf.no_ext_name())


li = os.listdir(parent)

for p in li:
    handleTheFile(p)
    # print(p)


def createXmlFile(bean: Selector):
    filename = targetPath + "/" + bean.filename
    print(filename)
    f = open(filename, 'w')
    doc = minidom.Document()
    root = doc.createElement("selector")
    root.setAttribute("xmlns:android", "http://schemas.android.com/apk/res/android")
    doc.appendChild(root)

    for state in bean.states:
        item = doc.createElement("item")
        if not (state.value == "normal"):
            item.setAttribute("android:state_%s" % state.value, "true")

        item.setAttribute("android:drawable", "@drawable/%s" % state.file_name)
        root.appendChild(item)

    print(doc.toprettyxml())
    doc.writexml(f, newl="\n", encoding='utf-8', addindent="    ")


for k in di.keys():
    # print("key = %s" % k)
    bean: Selector = di[k]
    # print("selector name : %s" % bean.filename)
    # print("selector values count : %d" % len(bean.states))
    createXmlFile(bean)
